package com.campus.exception;

import com.campus.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/11/6 8:36
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler({
            MissingServletRequestParameterException.class,
            MethodArgumentNotValidException.class,
            ConstraintViolationException.class
    })
    public Result<String> handleRequestValidation(HttpServletRequest req,
                                                  Exception e){
        log.info("url:{}  error: {}", req.getRequestURI(), e.getMessage()) ;
        return Result.error(400,e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public Result<String> handleException(HttpServletRequest req, Exception e){
        log.info("url:{}  type:{}, error: {}", req.getRequestURI(),
                e.getClass().getName(), e.getMessage()) ;
        return Result.error(500,"服务器出问题了，联系管理员");
    }




}
